﻿Imports System.Configuration
Imports System.Data.SqlClient
Imports SIGI.Common.BE

Public Class AppConfigDAC

#Region "<< -SearchById- >>"
    Public Shared Function SearchById(pKey As String) As AppConfigBE
        Dim wSqlConn As SqlConnection = Nothing
        Dim wSqlCmd As SqlCommand = Nothing
        Dim wReader As SqlDataReader = Nothing
        Dim wConnStr As String = String.Empty
        Dim wAppConfig As AppConfigBE = Nothing
        Try
            'Obtengo la cadena de conexión
            wConnStr = ConfigurationManager.ConnectionStrings("connString").ConnectionString
            'Creo la conexión a la BD
            wSqlConn = New SqlConnection(wConnStr)
            'Creo el comando
            wSqlCmd = New SqlCommand()
            wSqlCmd.Connection = wSqlConn
            wSqlCmd.CommandType = CommandType.StoredProcedure
            wSqlCmd.CommandText = "AppConfig_s_PorId"

            wSqlCmd.Parameters.Add("@Id", SqlDbType.NVarChar, 50)
            wSqlCmd.Parameters("@Id").Value = pKey

            'Abro la conexión a la BD
            wSqlConn.Open()

            'Ejecuto el comando
            wReader = wSqlCmd.ExecuteReader()
            'Obtengo el resultado, si es que hubo alguno.
            If wReader.Read() Then
                wAppConfig = New AppConfigBE
                wAppConfig.Id = pKey
                wAppConfig.Value = Convert.ToString(wReader("Valor"))
            End If

            'Cierro la conexión a la BD
            wSqlCmd.Clone()
        Catch ex As Exception
            Throw ex
        Finally
            wSqlConn = Nothing
            wSqlCmd = Nothing
            wReader = Nothing
        End Try

        Return wAppConfig
    End Function
#End Region
End Class
